package cn.sibu.myroot.listview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import cn.sibu.myroot.R;

/**
 * Created by 刘红亮 on 15-12-21 下午2:37.
 */
public class ContextListItem extends LinearLayout implements PopupMenu.OnMenuItemClickListener, View.OnClickListener {

    private PopupMenu mPopMenu;
    private TextView  mTextView;

    public ContextListItem(Context context) {
        super(context);

    }

    public ContextListItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ContextListItem(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mTextView = (TextView) findViewById(R.id.list_item_text);
        //附加单击处理程序
        View contextButton = findViewById(R.id.context);
        contextButton.setOnClickListener(this);

        //创建上下文菜单
        mPopMenu = new PopupMenu(getContext(),contextButton);
        mPopMenu.setOnMenuItemClickListener(this);
        mPopMenu.inflate(R.menu.context_menu);
    }

    @Override
    public void onClick(View v) {
        //处理上下文按钮单击以显示菜单
        mPopMenu.show();
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        String itemText = mTextView.getText().toString();
        switch (item.getItemId()){
            case R.id.menu_edit:
                Toast.makeText(getContext(),"Edit"+itemText,Toast.LENGTH_SHORT).show();
            break;
            case R.id.menu_delete:
                Toast.makeText(getContext(),"Delete"+itemText,Toast.LENGTH_SHORT).show();
            break;
        }
        return false;
    }
}
